﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.BLL;
using Wp.DAL;

public partial class Admin_Modules_Phote_PhotoEdit : System.Web.UI.Page
{
    public int? Id { get; set; }
    public int AlbumId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        // List<TblPhotoEntity> ls = new List<TblPhotoEntity>();
        if (Request["Id"] != null)
        {
            Id = Request["Id"].ToInt();
        }
        if (Request["AlbumId"] != null)
        {
            AlbumId = Request["AlbumId"].ToInt();
        }
        else
        {
            Response.Redirect("DefaultAlbum.aspx");
        }

        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        if (!Id.HasValue)
        {
            return;
        }
        TblPhotoEntity item = TblPhotoManager.CreateInstant().SelectOne(Id.Value);
        if (item != null)
        {
            txtName.Text = item.Name;
            txtSlug.Text = item.TextId;
            imgThumb.ImageUrl = item.ImageUrl;
            hdfimg.Value = item.ImageUrl;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        TblPhotoManager.CreateInstant().InsertUpdate(GetTblPhotoEntity());

        //int result = TblTermTaxonomyManager.CreateInstant().UpdateTag(item.Id, txtName.Text, txtSlug.Text, ddlparent.SelectedValue.ToInt(), txtDescription.Text, Taxonomy);
        //if (result == -1)
        //{
        //    lblError.Text = "An error occurred while updating the entries";
        //}
        //else if (result == -2)
        //{
        //    lblError.Text = "A term with the name provided already exists";
        //}
        //else
        //    lblError.Text = "Data was updated successfully";

    }
    TblPhotoEntity GetTblPhotoEntity()
    {
        TblPhotoEntity item = new TblPhotoEntity();
        if (Id.HasValue)
        {
            item = TblPhotoManager.CreateInstant().SelectOne(Id.Value);
        }
        else
        {
            item.TextId = TblPhotoManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text.Trim());
            item.CreatedBy = HttpContext.Current.User.Identity.Name;
            item.CreatedDate = DateTime.Now;
            item.DisplayOrder = 1;
            item.AlbumId = AlbumId;
        }
        item.Name = txtName.Text;
        item.ImageUrl = hdfimg.Value;
        item.ModifiedBy = HttpContext.Current.User.Identity.Name;
        item.ModifiedDate = DateTime.Now;
        return item;
    }
}